package com.telsoft.kqxs;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class LotteryResult extends ListActivity {
	/** Called when the activity is first created. */

	DBAdapter db = new DBAdapter(this);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);

		Bundle receivebundle = this.getIntent().getExtras();
		String x = receivebundle.getString("id");

		ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		if (isOnline()) {

			String xml = XMLfunctions
					.getXML("http://210.211.98.84/webicm/ts/ProvinceResult.xml?ProvinceID="
							+ x + "&TSM=227911");
			Document doc = XMLfunctions.XMLfromString(xml);

			// NodeList nodes = doc.getElementsByTagName("result");
			NodeList nodes = doc.getElementsByTagName("item");
			// Element e = (Element)nodes.item(1);

			for (int i = 0; i < nodes.getLength(); i++) {
				HashMap<String, String> map = new HashMap<String, String>();
				Node itemNode = nodes.item(i);

				map.put("id", x);
				Element definitionElement = (Element) itemNode;
				// Show Province Id
				NodeList wordDefinitionElements = (definitionElement)
						.getElementsByTagName("ProvinceCode");
				for (int j = 0; j < wordDefinitionElements.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements
							.item(j);
					map.put("ProvinceCode", XMLfunctions.getValue(
							wordDefinitionElement, "ProvinceCode"));
				}

				// Show Open Date
				NodeList wordDefinitionElements2 = (definitionElement)
						.getElementsByTagName("OpenDate");
				for (int j = 0; j < wordDefinitionElements2.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements2
							.item(j);
					map.put("OpenDate",
							"Ngày mở thưởng:"
									+ XMLfunctions.getValue(
											wordDefinitionElement, "OpenDate"));
				}

				// Show Special Result
				NodeList wordDefinitionElements3 = (definitionElement)
						.getElementsByTagName("Special_1");
				for (int j = 0; j < wordDefinitionElements3.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements3
							.item(j);
					map.put("Special_1",
							"Giải đặc biệt: "
									+ XMLfunctions.getValue(
											wordDefinitionElement, "Special_1"));
				}

				// Show First Result
				NodeList wordDefinitionElements4 = (definitionElement)
						.getElementsByTagName("First");
				for (int j = 0; j < wordDefinitionElements4.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements4
							.item(j);
					map.put("First",
							"First: "
									+ XMLfunctions.getValue(
											wordDefinitionElement, "First"));
				}

				// Show Second Result
				NodeList wordDefinitionElements5 = (definitionElement)
						.getElementsByTagName("Second");
				for (int j = 0; j < wordDefinitionElements5.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements5
							.item(j);
					map.put("Second",
							"Second: "
									+ XMLfunctions.getValue(
											wordDefinitionElement, "Second"));
				}

				// Show Third Result
				NodeList wordDefinitionElements6 = (definitionElement)
						.getElementsByTagName("Third");
				for (int j = 0; j < wordDefinitionElements6.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements6
							.item(j);
					map.put("Third",
							"Third: "
									+ XMLfunctions.getValue(
											wordDefinitionElement, "Third"));
				}

				// Show Fourth Result
				NodeList wordDefinitionElements7 = (definitionElement)
						.getElementsByTagName("Fourth");
				for (int j = 0; j < wordDefinitionElements7.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements7
							.item(j);
					map.put("Fourth",
							"Fourth: "
									+ XMLfunctions.getValue(
											wordDefinitionElement, "Fourth"));
				}

				// Show Fifth Result
				NodeList wordDefinitionElements8 = (definitionElement)
						.getElementsByTagName("Fifth");
				for (int j = 0; j < wordDefinitionElements8.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements8
							.item(j);
					map.put("Fifth",
							"Fifth: "
									+ XMLfunctions.getValue(
											wordDefinitionElement, "Fifth"));
				}

				// Show Sixth Result
				NodeList wordDefinitionElements9 = (definitionElement)
						.getElementsByTagName("Sixth");
				for (int j = 0; j < wordDefinitionElements9.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements9
							.item(j);
					map.put("Sixth",
							"Sixth: "
									+ XMLfunctions.getValue(
											wordDefinitionElement, "Sixth"));
				}

				// Show Seventh Result
				NodeList wordDefinitionElements10 = (definitionElement)
						.getElementsByTagName("Seventh");
				for (int j = 0; j < wordDefinitionElements10.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements10
							.item(j);
					map.put("Seventh",
							"Seventh: "
									+ XMLfunctions.getValue(
											wordDefinitionElement, "Seventh"));
				}

				// Show Eighth Result
				NodeList wordDefinitionElements11 = (definitionElement)
						.getElementsByTagName("Eighth");
				for (int j = 0; j < wordDefinitionElements11.getLength(); j++) {
					Element wordDefinitionElement = (Element) wordDefinitionElements11
							.item(j);
					map.put("Eighth",
							"Eighth: "
									+ XMLfunctions.getValue(
											wordDefinitionElement, "Eighth"));
				}
				mylist.add(map);

				/*
				 * Insert Lottery Result
				 */
				db.open();

				db.insertLotteryResult(x, map.get("ProvinceCode"),
						map.get("OpenDate"), map.get("Special_1"),
						map.get("First"), map.get("Second"), map.get("Third"),
						map.get("Fourth"), map.get("Fifth"), map.get("Sixth"),
						map.get("Seventh"), map.get("Eighth"));
				
				db.close();

			}
		} else {
			db.open();
			Cursor c = db.getPrize(x);
			if (c.moveToFirst()) {
				do {
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("id", c.getString(0));
					map.put("ProvinceCode", c.getString(1));
					map.put("OpenDate", c.getString(2));
					map.put("Special_1", c.getString(3));
					map.put("First", c.getString(4));
					map.put("Second", c.getString(5));
					map.put("Third", c.getString(6));
					map.put("Fourth", c.getString(7));
					map.put("Fifth", c.getString(8));
					map.put("Sixth", c.getString(9));
					map.put("Seventh", c.getString(10));
					map.put("Eighth", c.getString(11));

					mylist.add(map);
					// DisplayContact(c);
				} while (c.moveToNext());
			}

			db.close();
		}

		// Show Lottery Result
		ListAdapter adapter = new SimpleAdapter(this, mylist,
				R.layout.result_item,

				new String[] { "id", "ProvinceCode", "OpenDate", "Special_1",
						"First", "Second", "Third", "Fourth", "Fifth", "Sixth",
						"Seventh", "Eighth" }, new int[] { R.id.item_id,R.id.item_province_code,
						R.id.item_OpenDate, R.id.item_Special_1,
						R.id.item_First, R.id.item_Second, R.id.item_Third,
						R.id.item_Fourth, R.id.item_Fifth, R.id.item_Sixth,
						R.id.item_Seventh, R.id.item_Eighth });

		setListAdapter(adapter);

		final ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

			}
		});
	}

	// Check a Device's Network Connection
	public boolean isOnline() {
		ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		return (networkInfo != null && networkInfo.isConnected());
	}
}